//******************************************************************
//******************************************************************
//**********          ANts Peer To Peer Sources        *************
//
// ANts P2P realizes a third generation P2P net. It protects your
// privacy while you are connected and makes you not trackable, hiding
// your identity (ip) and crypting everything you are sending/receiving
// from others.

// Copyright (C) 2004  Roberto Rossi

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

package ants.p2p.http;

import java.net.*;
import java.util.*;

import ants.p2p.filesharing.*;

import org.apache.log4j.*;

public class HttpProxy extends Thread{
  int port = 8080;
  String nodeID;
  WarriorAnt wa;
  ServerSocket ss;

  static Logger _logger = Logger.getLogger(HttpProxy.class.getName());

  public ArrayList activeHandlers = new ArrayList();

  public HttpProxy(int port, String nodeID, WarriorAnt wa) throws Exception{
    this.nodeID = nodeID;
    this.port = port;
    this.wa = wa;
    ss = new ServerSocket(port);
    ss.setReuseAddress(true);
    this.start();
  }

  public HttpProxy(String nodeID, WarriorAnt wa) throws Exception{
    this.nodeID = nodeID;
    this.wa = wa;
    ss = new ServerSocket(port);
    ss.setReuseAddress(true);
    this.start();
  }

  public int getPort(){
    return port;
  }

  public String getNodeID(){
    return this.nodeID;
  }

  public WarriorAnt getWarriorAnt() {
    return this.wa;
  }

  public void run(){
    try{
      while(true){
        Socket listener = ss.accept();
        HttpRequestSender handler = new HttpRequestSender(listener, this);
        this.activeHandlers.add(handler);
        this.wa.propertyChangeSupport.addPropertyChangeListener(handler);
        handler.start();
      }
    }catch(Exception e){
      _logger.error("Proxy stopped (port: "+this.getPort()+")",e);
      try{
        this.wa.resetCurrentProxy();
      }catch(Exception ex){
        _logger.error("Error in closing port: "+this.getPort(),e);
      }
    }
  }

  public boolean terminate() throws Exception{
    ss.close();
    this.stop();
    this.join(5000);
    if(!this.isAlive() && ss.isClosed())
      return true;
    else
      return false;
  }

  public void removeHandler(HttpRequestSender handler){
    this.activeHandlers.remove(handler);
    this.wa.propertyChangeSupport.removePropertyChangeListener(handler);
  }
}
